<template>
  <div class="watch-out-box">
    <div class="watch-out-header">
      <div class="watch-out-title">{{ detail.title }}</div>
      <div>
        <el-radio-group v-model="activeName" size="mini">
          <el-radio-button label="process">考试中 ({{ stat.processCount }})</el-radio-button>
          <el-radio-button label="finished">已交卷 ({{ stat.handCount }})</el-radio-button>
          <el-radio-button label="apply">考试申请 ({{ stat.applyCount }})</el-radio-button>
        </el-radio-group>
      </div>
    </div>

    <watch-process v-if="activeName==='process'" :exam-id="examId" @stat="syncStat"/>
    <watch-hand v-if="activeName==='finished'" :exam-id="examId"/>
    <watch-apply v-if="activeName==='apply'" :exam-id="examId"/>

  </div>

</template>

<script>
import WatchProcess from '@/views/admin/exam/watch/components/WatchProcess'
import WatchHand from '@/views/admin/exam/watch/components/WatchHand'
import { simpleDetail } from '@/api/exam/exam'
import WatchApply from '@/views/admin/exam/watch/components/WatchApply'
import { watchStat } from '@/api/exam/watch'

export default {
  components: { WatchApply, WatchHand, WatchProcess },
  data() {
    return {
      examId: '',
      cacheKey: 'watch-tab-active',
      activeName: 'process',
      detail: {},
      stat: {
        requireCount: 0,
        processCount: 0,
        handCount: 0,
        missCount: 0,
        applyCount: 0
      },
      statTimer: null
    }
  },
  watch: {
    activeName: {
      handler(val) {
        localStorage.setItem(this.cacheKey, val)

        // 统计数据从里面传出
        if (val === 'process') {
          this.clearTimer()
        } else {
          // 初始化统计数据
          this.initTimer()
        }
      }
    }
  },

  created() {
    // 获取考试信息
    this.examId = this.$route.params.examId

    // 获取考试简要信息
    this.fetchDetail()

    // 获取缓存
    const active = localStorage.getItem(this.cacheKey)
    if (active) {
      this.activeName = active
    }
  },
  beforeDestroy() {
    this.clearTimer()
  },
  methods: {
    fetchDetail() {
      simpleDetail(this.examId).then(res => {
        this.detail = res.data
      })
    },
    // 获取统计信息
    fetchStat() {
      if (!this.examId) {
        return
      }
      watchStat({ id: this.examId }).then(res => {
        this.stat = res.data
      })
    },

    initTimer() {
      this.clearTimer()
      // 10秒获取一次
      this.statTimer = setInterval(this.fetchStat, 10000)
      // 获取一次信息
      this.fetchStat()
    },

    clearTimer() {
      if (this.statTimer) {
        clearInterval(this.statTimer)
      }
    },

    // 同步统计状态
    syncStat(data) {
      this.stat = data
    }
  }
}
</script>

<style scoped>

.watch-out-box{
  background: #1E2C3C;
  padding: 10px;
  height: calc(100vh - 100px);
  border-radius: 5px;
  overflow: auto;
  margin: -10px
}

.watch-out-header{
  display: flex;
  align-items: center;
  color: #fff;
  border-bottom: #888 1px solid;
  margin-bottom: 10px;
  height: 50px;
}

.watch-out-title{
  flex-grow: 1;
  font-size: 14px;
  font-weight: 700
}

</style>

